package cn.mijack.face;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by MiJack on 2016/6/15.
 */
public enum SHARE {
    WEIBO("com.sina.weibo", "com.sina.weibo.composerinde.ComposerDispatchActivity", "微博") {
        @Override
        Intent generateIntent(String shareContent, String fname) {
            Intent intent = new Intent();
            File file = new File(fname);
            if (file.exists()) {
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            intent.putExtra(Intent.EXTRA_TEXT, shareContent);
            intent.setType("text/plain");
            return intent;
        }
    },
    WECHAT("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", "微信"){
        @Override
        Intent generateIntent(String shareContent, String fname) {
            Intent intent = new Intent();
            File file = new File(fname);
            if (file.exists()) {
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            intent.setType("image/*");
            return intent;
        }
    },
    CIRCLE("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI", "微信"){
        @Override
        Intent generateIntent(String shareContent, String fname) {
            Intent intent = new Intent();
            File file = new File(fname);
            if (file.exists()) {
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            intent.setType("image/*");  intent.putExtra("Kdescription", shareContent);
            return intent;
        }
    };
    public String packageName;
    public String activityName;
    private String applicationName;

    SHARE(String packageName, String activityName, String applicationName) {
        this.packageName = packageName;
        this.activityName = activityName;
        this.applicationName = applicationName;
    }

    public void share(Activity context, String shareContent) {
//截图
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
        String fname = "/sdcard/" + sdf.format(new Date()) + ".png";
        View view = ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);


        view.setDrawingCacheEnabled(true);

        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

        if (bitmap != null) {
            System.out.println("bitmap got!");
            try {
                FileOutputStream out = new FileOutputStream(fname);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                System.out.println("file" + fname + "output done.");


                Intent intent = generateIntent(shareContent, fname);
                intent.setAction(Intent.ACTION_SEND);
                intent.setClassName(packageName, activityName);

                PackageManager packageManager = context.getPackageManager();
                List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
                if (resolveInfos == null || resolveInfos.size() == 0) {
                    Toast.makeText(context, applicationName + "未安装", Toast.LENGTH_SHORT).show();
                } else {
                    context.startActivity(intent);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("bitmap is NULL !");
        }


//        Intent shareIntent = new Intent();
//        shareIntent.setAction(Intent.ACTION_SEND);
//        shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);
//        shareIntent.setType("text/plain");
//        context.startActivity(Intent.createChooser(shareIntent,"Hello "));
//

//        shareIntent.setClassName(packageName,activityName);
//        PackageManager packageManager = context.getPackageManager();
//        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(shareIntent, 0);
//        if (resolveInfos == null || resolveInfos.size() == 0) {
//            Toast.makeText(context,applicationName+"未安装",Toast.LENGTH_SHORT).show();
//        } else {
//            context.startActivity(shareIntent);
//        }
    }

    abstract Intent generateIntent(String shareContent, String fname);
}
